﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Demo.Services.Contracts.DTO.Infos;
using System.Data;
using Demo.CommonBase.Logging;
using System.Reflection;
using Demo.CommonBase.Exceptions;
using Demo.CommonBase.DTO.Infos;

namespace Demo.Services.Integration.DAO.IntegrationComponents.Mappings
{
    public class ResourceDAOMapping
    {
        /// <summary>
        /// Obtener una Lista de Tecnologias
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static ResourceResultInfo GetResourceList(DataSet ds)
        {
            ResourceResultInfo result;
            List<ResourceInfo> resourceList;
            try
            {
                Logger.Info(MethodInfo.GetCurrentMethod().ToString());
                DataTable dt = ds.Tables[1];

                resourceList = (from resource in dt.AsEnumerable()
                          select new ResourceInfo
                          {
                              Id = resource.Field<int>("id"),
                              Name = resource.Field<string>("nombre"),
                              LastName = resource.Field<string>("apellidoPaterno"),
                              SecondLastName = resource.Field<string>("apellidoMaterno"),
                              BirthDate = resource.Field<DateTime>("fechaNacimiento").ToString("dd/MM/yyyy"),                              
                              Cost = resource.Field<decimal>("costo"),
                              PhoneNumber = resource.Field<string>("telefono")
                          }).ToList();
                result = new ResourceResultInfo();
                result.ResourceList = resourceList;
                result.TotalReg = Convert.ToInt32(ds.Tables[0].Rows[0]["TotalReg"]);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

            return result;
        }

        /// <summary>
        /// Obtener una tecnologia.
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static ResourceInfo GetResourceById(DataSet ds)
        {
            ResourceInfo result;
            try
            {
                Logger.Info(MethodInfo.GetCurrentMethod().ToString());
                DataTable dt = ds.Tables[0];

                result = (from resource in dt.AsEnumerable()
                          select new ResourceInfo
                          {
                              Id = resource.Field<int>("id"),
                              Name = resource.Field<string>("nombre"),
                              LastName = resource.Field<string>("apellidoPaterno"),
                              SecondLastName = resource.Field<string>("apellidoMaterno"),
                              BirthDate = resource.Field<DateTime>("fechaNacimiento").ToString("dd/MM/yyyy"),
                              ResourceType = resource.Field<int>("tipoRecursoId"),
                              PhoneNumber = resource.Field<string>("telefono"),
                              Cost = resource.Field<decimal>("costo"),
                              TypeLevel = resource.Field<string>("TipoRecurso")

                          }).ToList()[0];
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return result;
        }

        public static List<ProjectReportInfo> GetResourceAssignment(DataSet ds)
        {
            List<ProjectReportInfo> result;
            DataRow dataRow;
            try
            {
                Logger.Info();
                result = new List<ProjectReportInfo>();
                dataRow = ds.Tables[0].Rows[0];
                result.Add(GetAssignment(dataRow, 1));
                result.Add(GetAssignment(dataRow, 2));
                
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

            return result;

        }


        private static ProjectReportInfo GetAssignment(DataRow row, int index)
        {
            ProjectReportInfo assignment;
            try
            {
                assignment = new ProjectReportInfo();
                switch (index)
                {
                    case 1:
                        assignment.Name = "Asignados";
                        assignment.ResourcesNumber = (int)row["asignados"];
                        break;
                    case 2:
                        assignment.Name = "Libres";
                        assignment.ResourcesNumber = (int)row["libres"];
                        break;
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return assignment;
        }

       
    }
}
